package com.itboye.jinpuyiyuan;


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.itboye.jinpuyiyuan.activity.login.LoginOrRegisterActivity;
import com.itboye.jinpuyiyuan.app.MyApplication;
import com.itboye.jinpuyiyuan.base.BaseActivity;
import com.itboye.jinpuyiyuan.fragment.HomeFragment;
import com.itboye.jinpuyiyuan.fragment.MeDoctorFragment;
import com.itboye.jinpuyiyuan.fragment.MeFragment;
import com.itboye.jinpuyiyuan.fragment.MessageFragment;
import com.itboye.jinpuyiyuan.utils.IMKit.LoginSampleHelper;
import com.itboye.jinpuyiyuan.utils.logincontroller.LoginController;
import com.itboye.jinpuyiyuan.utils.logincontroller.UnLoginState;
import com.itboye.jinpuyiyuan.utils.sputil.Const;
import com.itboye.jinpuyiyuan.utils.sputil.SPUtils;
import com.itboye.jinpuyiyuan.widget.FilterUtil;
import com.itboye.jinpuyiyuan.widget.loadingutil.MAlert;

public class MainActivity extends BaseActivity {
    private HomeFragment homeFragment;
    private MeFragment meFragment;
    private MeDoctorFragment meDoctorFragment;
    private MessageFragment messageFragment;
    private FragmentManager fragmentManager;
    private FrameLayout content;
    private ImageView home_image,message_image,me_image;
    private TextView home_text,message_text,me_text;
    private RelativeLayout home_layout,message_layout,me_layout;
    private int id;//1 医生为8 2 患者为15
    private String uid;//用户id
    //登录和退出登录的广播
    private BroadcastReceiver loginReceiver;
    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.activity_mains);
        initViewData();
        initBrodacast();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().setStatusBarColor(getResources().getColor(R.color.title_blue));
        }


    }

    private void initBrodacast() {
        loginReceiver= new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                initViewData();
            }
        };
        registerReceiver(loginReceiver,new IntentFilter(FilterUtil.LOGIN));
        registerReceiver(loginReceiver,new IntentFilter(FilterUtil.EXITLOGIN));
    }

    private void initViewData() {
        uid= (String) SPUtils.get(getApplication(),null,Const.UID,"");
        id= (int) SPUtils.get(getApplication(),null,"roles_doctor",0);
        fragmentManager = getSupportFragmentManager();
        //判断医生端还是患者端
        if (id==8){
            home_layout.setVisibility(View.GONE);
            setTabSelection(1);
        }else {
            home_layout.setVisibility(View.VISIBLE);
            setTabSelection(0);
        }

    }


    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()){
            case R.id.home_layout:
            case R.id.home_image:
            case R.id.home_text:
                setTabSelection(0);
                break;
            case R.id.message_layout:
            case R.id.message_image:
            case R.id.message_text:
                setTabSelection(1);
                break;
            case R.id.me_layout:
            case R.id.me_image:
            case R.id.me_text:
                setTabSelection(2);
                break;
        }
    }

    public void setTabSelection(int index){
        // 每次选中之前先清楚掉上次的选中状态
        clearSelection();
        // 开启一个Fragment事务
        FragmentTransaction transaction=fragmentManager.beginTransaction();
        // 先隐藏掉所有的Fragment，以防止有多个Fragment显示在界面上的情况
        hideFragments(transaction);
        switch (index) {
            case 0://首页

                home_image.setImageResource(R.drawable.shouye_new);
                home_text.setTextColor(getResources().getColor(R.color.title_blue));
                if (homeFragment == null) {
                    //如果HomeFragment为空，则创建一个添加到界面
                    homeFragment = new HomeFragment();
                    transaction.add(R.id.content,homeFragment);

                }else{
                    transaction.show(homeFragment);

                }
                break;
            case 1://消息
                if (uid.equals("")||uid==null){
                    startActivity(new Intent(this, LoginOrRegisterActivity.class));
                    return;
                }
                message_image.setImageResource(R.drawable.xiaoxi_new);
                message_text.setTextColor(getResources().getColor(R.color.title_blue));
                if (messageFragment == null) {
                    //如果IntegralFragment为空，则创建一个添加到界面
                    messageFragment = new MessageFragment();
                    transaction.add(R.id.content,messageFragment);
//                    transaction.replace(R.id.content,messageFragment);

                }else{
                    transaction.show(messageFragment);

                }
                break;
            case 2://我的
                me_image.setImageResource(R.drawable.wode_new);
                me_text.setTextColor(getResources().getColor(R.color.title_blue));
//                transaction.replace(R.id.content,new MeFragment());
                if (id==8){
                    if (meDoctorFragment == null) {
                        //如果ShopCartFragment为空，则创建一个添加到界面
                        meDoctorFragment = new MeDoctorFragment();
                        transaction.add(R.id.content,meDoctorFragment);

                    }else{
                        transaction.show(meDoctorFragment);

                    }

                }else {
                    if (meFragment == null) {
                        //如果ShopCartFragment为空，则创建一个添加到界面
                        meFragment = new MeFragment();
                        transaction.add(R.id.content,meFragment);

                    }else{
                        transaction.show(meFragment);

                    }
                }
                break;
            default:

                break;
        }
        transaction.commitAllowingStateLoss();
    }
    /**
     * 将所有的Fragment都置为隐藏状态。
     * @param transaction 用于对Fragment执行操作的事务
     */
    private void hideFragments(FragmentTransaction transaction) {
        if (homeFragment != null) {
            transaction.hide(homeFragment);
        }
        if (messageFragment != null) {
            transaction.hide(messageFragment);
        }

        if (meFragment != null) {
            transaction.hide(meFragment);
        }
        if (meDoctorFragment != null) {
            transaction.hide(meDoctorFragment);
        }
    }

    /**
     * 清除掉所有的选中状态。
     */
    private void clearSelection() {
        home_image.setImageResource(R.drawable.shouye);
        home_text.setTextColor(getResources().getColor(R.color.black));
        message_image.setImageResource(R.drawable.xiaoxi);
        message_text.setTextColor(getResources().getColor(R.color.black));
        me_image.setImageResource(R.drawable.wod);
        me_text.setTextColor(getResources().getColor(R.color.black));
    }

    //记录用户首次点击返回键的时间
    private long firstTime=0;

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        switch (keyCode){
            case KeyEvent.KEYCODE_BACK:
                long secondTime=System.currentTimeMillis();
                if(secondTime-firstTime>2000){
                    Toast.makeText(MainActivity.this,"再按一次退出程序",Toast.LENGTH_SHORT).show();
                    firstTime=secondTime;
                    return true;
                }else{
                    MyApplication.instatnce.removeAllActivity();
                    System.exit(0);

                }
                break;
        }
        return super.onKeyUp(keyCode, event);
    }

}
